from TimeChessEngine import *
from TimeChessUtil import *
import sys
from random import choice

class BranchingFactorTester(object):
	def __init__(self):
		self.engine = TimeChessEngine()

	def getAllMoves(self):
		def canMove(p):
			return ((p.pos.t == self.engine.t) and (p.color==self.engine.currentSide))

		moves = []
		for pos in self.engine.game.getPosWhereTrue(canMove):
			[moves.append(m) for m in self.engine.getPossibleMoves(pos)]

		return moves

	def gatherData(self,filename):
		f = open(filename,"w")

		seenzero = False
		while True:
			if self.engine.t == 0:
				if not seenzero:
					seenzero = True
				else:
					break

			moves = self.getAllMoves()
			print "%s,%s"%(self.engine.t, len(moves))
			f.write("%s,%s\n"%(self.engine.t, len(moves)))

			self.engine.attemptMove(choice(moves))



if __name__=="__main__":
	t = BranchingFactorTester()
	t.gatherData(sys.argv[1])
